//
//  NetWorkingHelper.m
//  ZXFrameWork
//
//  Created by ??∫‰∫´ on 14-8-16.
//  Copyright (c) 2014Âπ? ??∫‰∫´. All rights reserved.
//

#import "NetWorking.h"


@implementation NetWorking


+ (NSDictionary*)send:(NSString *)url
               method:(RequestMethod)requestMethod
        requestHeader:(NSDictionary *)header
          requestBody:(NSDictionary *)body{
    
//        FeSDDLogDebug(@"Request parameter:%@",body);
    NSMutableString *responseString = nil;
    if (NotReachable == [[Reachability reachabilityForInternetConnection]currentReachabilityStatus]) {
//        responseString = [NSString stringWithFormat:kHTTPRequestFormater,kNoNetworkResultCode,kNoNetworkResultMsg];
    }else {
        
        if (requestMethod == POST)
        {
            ASIFormDataRequest *formDataRequest = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url]];
            [formDataRequest setRequestMethod:@"POST"];
          
            formDataRequest.timeOutSeconds = 10;
//            formDataRequest.shouldAttemptPersistentConnection = NO;
//            [formDataRequest setValidatesSecureCertificate:NO]; 
//            [formDataRequest setSslSecurityLevel:(CFStringRef *)kCFStreamSocketSecurityLevelSSLv3];
            NSEnumerator *keyEnum = [header keyEnumerator];
            NSString *keyName = nil;
            while ((keyName = keyEnum.nextObject) != nil) {
                [formDataRequest addRequestHeader:keyName value:[header objectForKey:keyName]];
            }
            keyName = nil;
            NSEnumerator *bodyEnum = [body keyEnumerator];
            while ((keyName = bodyEnum.nextObject) != nil) {
                if ([keyName isEqualToString:@"file"]) {
                    [formDataRequest setFile:[body objectForKey:keyName] forKey:keyName];
                }else{
                    [formDataRequest setPostValue:[body objectForKey:keyName] forKey:keyName];
                }
            }
                                      
//            [formDataRequest setProxyUsername:@"yan.jm"];
//            [formDataRequest setProxyPassword:@"1qaz@*&WSX"];
            [formDataRequest buildPostBody];
            formDataRequest.uploadProgressDelegate = nil;
            [formDataRequest startSynchronous];
        
            
            NSError *error = [formDataRequest error];
            if (nil == error) {
//                 NSLog(@"--%@",formDataRequest.url);
                NSString *tmpResponseString = [[NSString alloc] initWithData:[formDataRequest responseData] encoding:NSUTF8StringEncoding];
                
                if (tmpResponseString != nil && ![@"" isEqualToString:tmpResponseString]) {
                    
                    responseString = [NSMutableString stringWithFormat:@"%@",tmpResponseString];

                    responseString = [NSMutableString stringWithFormat:@"%@",tmpResponseString];
                    //    FeSDDLogDebug(@"responseJsonDic:%@",result);

                }
            }else{
                                 NSLog(@"没有网络formDataRequest.url=%@,error=%@",formDataRequest.url,error);
            }
            if (nil == responseString || [@"" isEqualToString:responseString]) {
//                responseString = [NSString stringWithFormat:kHTTPRequestFormater,kEmptyResultCode,kEmptyResultMsg];
            }
            
            
        }
    }


    NSDictionary * result = [responseString objectFromJSONString];

    return result;
    
    
}

@end
